package com.damie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dadie.Domain.UserDomain;
import com.dadie.Dto.UserDto;
import com.dadie.parame.UserParame;
import com.damie.mapper.UserMapper;
import com.damie.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.damie.uilt.Copy;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDomain> implements UserService {

    @Resource
    private UserMapper mapper;

    @Override
    public Page<UserDto> selectAllUser(Integer index, Integer size, UserParame userParame) {
        QueryWrapper<UserDomain> userDomainQueryWrapper = new QueryWrapper<>();
        if (userParame.getUserPassword()!=null){
            userDomainQueryWrapper.like("user_password",userParame.getUserPassword());
        }
        if (userParame.getUserRole()!=null){
            userDomainQueryWrapper.eq("user_role",userParame.getUserRole());
        }
        if (userParame.getUserCreateTime()!=null){
            userDomainQueryWrapper.ge("user_create_time",userParame.getUserCreateTime());
        }
        Page<UserDomain> userDomainPage = mapper.selectPage(new Page<UserDomain>(index, size), userDomainQueryWrapper);
        Page<UserDto> userDtoPage = new Page<>();
        List<UserDto> collect = userDomainPage.getRecords().stream().map(userDomain -> {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userDomain, userDto);
            return userDto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(userDomainPage,userDtoPage);
        userDtoPage.setRecords(collect);
        return userDtoPage;
    }


    @Override
    public UserDto selectByIdUser(String id) {
        QueryWrapper<UserDomain> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",id);
        UserDomain userDomain = mapper.selectOne(objectQueryWrapper);
        UserDto userDto = new UserDto();
        System.out.println(userDomain+":"+userDto);
        BeanUtils.copyProperties(userDomain,userDto);
        return userDto;
    }

    @Override
    public UserDto deleteByIdUser(String id) {
        QueryWrapper<UserDomain> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",id);
        UserDomain userDomain = mapper.selectOne(objectQueryWrapper);
        if (ObjectUtils.isNotEmpty(userDomain)){
            int i = mapper.delete(objectQueryWrapper);
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userDomain,userDto);
            return userDto;
        }
        throw new RuntimeException("删除出错");
    }

    @Override
    public UserDto updateByIdUser(UserParame userParame) {
        UserDomain userDomain = new UserDomain();
        if (userParame.getSalt()!=null){
            userDomain.setSalt(userParame.getSalt());
        }
        if (userParame.getUserPassword()!=null){
            userDomain.setUserPassword(userParame.getUserPassword());
        }
        if (userParame.getUserTel()!=null){
            userDomain.setUserTel(userParame.getUserTel());
        }
        userDomain.setUserId(userParame.getUserId());
        int i = mapper.updateById(userDomain);
        return(UserDto) Copy.copy(userDomain,new UserDto());
    }
}
